package com.crazy.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * @author panyonghui
 * @describe 文件压缩工具类
 * @date 2022/6/28 11:27
 */
public class ZipUtils {

    /**
     * @describe 文件压缩
     * @auther crazy_cat
     * @date 2022/6/28 14:07
     * @param srcPath 文件路径（此文件路径下的全部文件将被压缩，不包含文件夹）/home/common/ 或 /home/common/my_file.jpg
     * @param targetPath zip包存放路径，当srcPath传的是文件夹时，targetPath的文件不能在srcPath同一级目录/target/file/test.zip
     * @return void
     * @modify
     */
    public static void filesToZip(String srcPath, String targetPath) throws IOException {
        File srcFile = new File(srcPath);
        if (srcFile.isFile()){
            try (OutputStream outputStream = new FileOutputStream(targetPath);
                 ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);) {
                InputStream inputStream = new FileInputStream(srcFile);
                writeZipStream(zipOutputStream, inputStream, srcFile);
            } catch (IOException e) {
                throw e;
            }
        } else if (srcFile.isDirectory()) {
            try (OutputStream outputStream = new FileOutputStream(targetPath);
                 ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);) {
                InputStream inputStream = null;
                File[] srcFiles = srcFile.listFiles();
                for (File src : srcFiles) {
                    inputStream = new FileInputStream(src);
                    writeZipStream(zipOutputStream, inputStream, src);
                }
            } catch (IOException e) {
                throw e;
            }
        }
    }

    /**
     * @describe 写入文件流
     * @auther crazy_cat
     * @date 2022/6/28 14:15
     * @param zipOutputStream
     * @param inputStream
     * @param src
     * @return void
     * @modify
     */
    private static void writeZipStream(ZipOutputStream zipOutputStream, InputStream inputStream, File src) throws IOException {
        zipOutputStream.putNextEntry(new ZipEntry(src.getName()));
        byte[] b = new byte[1024];
        int len;
        while ((len = inputStream.read(b)) != -1) {
            zipOutputStream.write(b, 0, len);
        }
        zipOutputStream.flush();
    }

    /**
     * @describe 解压文件
     * @auther crazy_cat
     * @date 2022/6/28 14:31
     * @param filePath zip文件路径 /target/file/test.zip
     * @param descDir 解压后文件存放目录 /home/common/
     * @return void
     * @modify
     */
    public static void unZipFiles(String filePath, String descDir) throws IOException {
        try {
            File sourceFile = new File(filePath);
            ZipFile zipFile = new ZipFile(sourceFile);
            Enumeration entries = zipFile.entries();
            while(entries.hasMoreElements()){
                ZipEntry entry= (ZipEntry) entries.nextElement();
                String zipEntryName = entry.getName();
                InputStream inputStream = zipFile.getInputStream(entry);
                OutputStream outputStream = new FileOutputStream(descDir + zipEntryName);
                byte[] b = new byte[1024];
                int len;
                while((len = inputStream.read(b)) != -1){
                    outputStream.write(b,0, len);
                }
                inputStream.close();
                outputStream.close();
            }
        } catch (Exception e) {
            throw e;
        }
    }

    public static void main(String[] args) throws IOException {
        String srcPath = "E:\\home\\test\\22dddc962d4ea10b9aaf6c67832c05e7.pdf";
        String targetPath = "E:\\home\\test.zip";
        String unzip = "E:\\home\\srbank\\";
        filesToZip(srcPath, targetPath);
        unZipFiles(targetPath, unzip);
    }
}
